<?php

// $Id: vpage_news_article_form_ctrl.class.php 1570 2009-01-13 12:44:02Z ogrebg $

require_once(TANGRA_MAIN_DIR.'form/gform_ctrl_with_obj.class.php');

require_once('vpage_news_article_form.class.php');
require_once('vpage_news_article_form_view.class.php');

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpt/vpage_news_article_dbc.class.php');


class VPage_News_Article_Form_Ctrl extends GForm_Ctrl_With_Object {
	private $attributes;

	function __construct($system_name, Vars_Manager $vm, DB_Connection $dbc, $object_id_capture = 'obj_id', $attributes) {
		$this->attributes = $attributes;
		parent::__construct($system_name, $vm, $dbc, $object_id_capture);
	}


	protected function create_form() {
		$form = new VPage_News_Article_Form($this->attributes);

//		$form->generate_html();
		return $form;
	}


	protected function create_form_view() {
		$ret = new VPage_News_Article_Form_View($this->form);

		return $ret;
	}


	protected function on_good_submit() {
		$ret = true;

		$form = $this->get_form();
		if ($form->get_field_value('title') != strip_tags($form->get_field_value('title'))) {
			$form->set_field_error('title', 'contains_html');
			$ret = false;
		}

		return $ret;
	}


	protected function create_object() {
		$object = new VPage_News_Article_DBC();

		return $object;
	}


	protected function transfer_obj2form() {
		$object = $this->get_object();
		$form = $this->get_form();

		$form->set_field_value('title', $object->get_title());
		$form->set_field_value('content', $object->get_content());
		$form->set_field_value('intro', $object->get_intro());
	}


	protected function transfer_form2obj() {
		$object = $this->get_object();
		$form = $this->get_form();

		$object->set_title($form->get_field_value('title'));
		$object->set_content($form->get_field_value('content'));
		$object->set_intro($form->get_field_value('intro'));
	}


	protected function _save_object(DB_Connection $dbc, I_DB_Storable $object) {
		$object->save($dbc, $this->get_permanent_external_param('user_id'));
	}
}